package managedBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import org.hibernate.Session;
import org.hibernate.Transaction;
import conexao.HibernateUtil;
import model.Contato;

@ManagedBean(name="contatoBean")
public class ContatoBean {
	
	Contato c;
	private String dataCad;
	
	public ContatoBean(){
		c = new Contato();
	}
	
	public void gravar() throws ParseException{
		Session session = null;
		Transaction transacao = null;
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date dataForm = sdf.parse(dataCad.toString());
		c.setDtCadastro(dataForm);
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transacao = session.beginTransaction();
			session.save(c);
			transacao.commit();	
		} catch (Exception e) {
			System.out.println("Deu erro "+e.getMessage());
			e.printStackTrace();
		}
		
		System.out.println("teste do botao gravar");
		System.out.println("nome: "+c.getNome());
		System.out.println("Telefone: "+c.getTelefone());
	}
	
	
	//Getters and Setters
	public Contato getC() {
		return c;
	}
	public void setC(Contato c) {
		this.c = c;
	}

	public String getDataCad() {
		return dataCad;
	}

	public void setDataCad(String dataCad) {
		this.dataCad = dataCad;
	}
	
}
